<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">标题栏设置</div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="标题">
          <el-input v-model="form['wxappTitle']"/>
        </el-form-item>
        <el-form-item label="标题栏颜色">
          <el-color-picker v-model="form['mainColor']" :predefine="predefineColors"/>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'HomepageSlogan',
    data() {
      return {
        loading: false,
        // 预定义颜色
        predefineColors: [
          '#19C5F4',
          '#ff8c00',
          '#ffd700',
          '#90ee90',
          '#00ced1',
          '#c71585'
        ],
        form: {
          // 标题
          wxappTitle: '',
          // 颜色
          mainColor: '#19C5F4'
        }
      }
    },
    mounted(){
      this.loadData();
    },
    methods: {
      loadData() {
        (async () => {
          const result = await this.$service.applicationGetWxAppTitle();
          // const result = await this.$service.applicationToRequestWxCloudApi({action: 'getWxappStyle'});
          if (result.code === 0) {
            // Object.assign(this.form, result.data['list'] || {});
            Object.assign(this.form, result.data || {});
          }
        })();
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            (async () => {
              const params = Object.assign({}, this.form);
              const result = await this.$service.applicationSaveWxAppTitle(params);
              // const result = await this.$service.applicationToRequestWxCloudApi({
              //   action: 'updateWxappStyle',
              //   params: JSON.stringify(params)
              // });
              if (result.code === 0) {
                this.$message.success('操作成功~');
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

